package com.sosee.cgfw.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import com.sosee.cgfw.R;
import com.sosee.cgfw.adapter.InvitationAdapter;
import com.sosee.cgfw.entity.PiaoJia;
import com.sosee.cgfw.entity.PiaoJiaData;
import com.sosee.cgfw.entity.PiaoJiaJson;
import com.sosee.cgfw.interfac.InvitationInterface;
import com.sosee.cgfw.utils.Constans;
import com.sosee.cgfw.utils.LogUtils;
import com.sosee.cgfw.utils.Utils;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;
import com.umeng.analytics.MobclickAgent;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Thinkpad on 2016/2/24.
 * //邀请函 activity
 */
public class InvitationActivity extends Activity implements AdapterView.OnItemClickListener, InvitationInterface {
    private ListView lv;
    private InvitationAdapter mAdapter;
    private HttpUtils httpUtils;
    private List<PiaoJiaData> list=new ArrayList<PiaoJiaData>();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_invitation);
        initView();
        initData();
    }

    @Override
    protected void onResume() {
        super.onResume();
        MobclickAgent.onResume(this);
    }

    @Override
    protected void onPause() {
        super.onPause();
        MobclickAgent.onPause(this);
    }

    public void initView(){
        lv= (ListView) findViewById(R.id.lv);
        mAdapter=new InvitationAdapter(this);
        mAdapter.setList(Utils.piaoJiaList);
        mAdapter.setAnInterface(this);
        lv.setAdapter(mAdapter);
        lv.setOnItemClickListener(this);
    }

    public void initData(){
        list=Utils.piaoJiaList;
        httpUtils=new HttpUtils();
        httpUtils.configCurrentHttpCacheExpiry(10);
        mAdapter.setList(list);
        LogUtils.log_E("yaoqinghan:" + Utils.piaoJiaList.size());
        mAdapter.notifyDataSetChanged();
    }



    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

    }

    @Override
    public void onClick(String id) {
        httpUtils.send(HttpRequest.HttpMethod.GET, Constans.YAOQINGHAN_URL + id, new RequestCallBack<String>() {
            @Override
            public void onSuccess(ResponseInfo<String> responseInfo) {
                JSONObject jsonObject = null;
                try {
                    jsonObject = new JSONObject(responseInfo.result);
                    String status = jsonObject.getString("status");
                    if (status != null && status.equals("success")) {
                        Utils.showToast(getString(R.string.acceptYaoqing_success));
                        initData_web();
                    } else {
                        String err = jsonObject.getString("error");
                        if ("没有找到相应的邀请函信息".equals(err)) {
                            Utils.showToast(getString(R.string.Yaoqing_guoqi));
                        } else {
                            Utils.showToast(err);
                        }
                    }


                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFailure(HttpException e, String s) {
                Utils.showToast(getString(R.string.acceptYaoqing_failed));
            }
        });

    }


    @Override
    public void onBackPressed() {
        startActivity(new Intent(this,AssistantActivity.class));
        finish();
    }

    public void back(View view){
        startActivity(new Intent(this,AssistantActivity.class));
        finish();
    }


    public void initData_web(){
        httpUtils.configCurrentHttpCacheExpiry(1000 * 1);
        httpUtils.send(HttpRequest.HttpMethod.GET, Constans.PIAO_URL, new RequestCallBack<String>() {
            @Override
            public void onSuccess(ResponseInfo<String> responseInfo) {
                PiaoJiaJson pjson = com.alibaba.fastjson.JSONObject.parseObject(responseInfo.result, PiaoJiaJson.class);
                List<PiaoJia> list = pjson.getResult();
                if (list != null) {
                    Utils.showToast("jilu +" + list.size());
                } else {
                    Utils.showToast("解析json出错");
                }
                initUI(list);

            }

            @Override
            public void onFailure(HttpException e, String s) {
                Utils.showToast("网络异常" + s);
            }
        });
    }

    public void initUI(List<PiaoJia> mlist){
        list.clear();
        for (int i=0;i<mlist.size();i++){
            PiaoJiaData pdata=mlist.get(i).getData();
            String status=pdata.getStatus();
            if(status!=null){
                LogUtils.log_E("status="+status+"===");
               if(status.equals("2")||status.equals("3")){
                    list.add(pdata);
                }
            }else{
            }

        }
        mAdapter.setList(list);
        mAdapter.notifyDataSetChanged();


    }
}
